*************************************************************
* Replication codes for analyses in Section 5.1 of
* Beyond Yield Response: Weather Shocks and Crop Abandonment
* May 2020
*************************************************************

	use dta\dta_for_price, clear

* bootstrap corn price reg (IV)	
	
	program iv_price_corn

		qui reghdfe	corn_recprice_state_real L_corn_acres_state L_corn_stockvar c.year##c.year ///
			if acres_grain_ratio_harvest!=. & irr_ratio < .1 & drop_corn==0 & pl_yrs_corn>5, ///
			absorb(fips) 
		qui predict iv_corn_recprice_state_real
			
		qui egen ivpc_median0 = median(iv_corn_recprice_state_real) if irr_ratio < .1 & drop_corn==0 & pl_yrs_corn>5
		qui gen pc = iv_corn_recprice_state_real - ivpc_median0	
		qui gen precXpc = prec*pc
		qui gen prec2Xpc = prec2*pc
		qui gen gdd8_26Xpc = gdd8_26*pc
		qui gen gdd26_35Xpc = gdd26_35*pc
		qui gen dday35CXpc = dday35C*pc

		
		qui reghdfe acres_grain_ratio_harvest prec precXpc prec2 prec2Xpc gdd8_26 gdd8_26Xpc gdd26_35 gdd26_35Xpc dday35C dday35CXpc ///
			i.stateansi#c.t i.stateansi#c.t2 yeart* if irr_ratio < .1 & drop_corn==0 & pl_yrs_corn>5, ///
			absorb(fips) 
		
		drop pc	ivpc_median0 *Xpc iv_corn_recprice_state_real
		
	end
	
	xtset, clear
	bootstrap, cluster(stateyear) reps(10): iv_price_corn		
	estimates store iv_price_corn

		* obtain effects evaluated at knots for plotting splines (Appendix Figure A10)
		lincom (gdd8_26 + 0.86*gdd8_26Xpc)/100*(26-8)
		lincom (gdd8_26 + 0.86*gdd8_26Xpc)/100*(26-8) + (gdd26_35 + 0.86*gdd26_35Xpc)*(35-26)
		lincom (gdd8_26 + 0.86*gdd8_26Xpc)/100*(26-8) + (gdd26_35 + 0.86*gdd26_35Xpc)*(35-26) + (dday35C + 0.86*dday35CXpc)*(41-35)

		lincom (gdd8_26 - 0.86*gdd8_26Xpc)/100*(26-8)
		lincom (gdd8_26 - 0.86*gdd8_26Xpc)/100*(26-8) + (gdd26_35 - 0.86*gdd26_35Xpc)*(35-26)
		lincom (gdd8_26 - 0.86*gdd8_26Xpc)/100*(26-8) + (gdd26_35 - 0.86*gdd26_35Xpc)*(35-26) + (dday35C - 0.86*dday35CXpc)*(41-35)
		

* bootstrap soy price reg (IV)	
	program iv_price_soy

		qui reghdfe	soy_recprice_state_real L_soy_acres_state L_soy_stockvar c.year##c.year ///
			if acres_grain_ratio_harvest!=. & irr_ratio < .1 & drop_soy==0 & pl_yrs_soy>5, ///
			absorb(fips) 
		qui predict iv_soy_recprice_state_real
			
		qui egen ivps_median0 = median(iv_soy_recprice_state_real) if irr_ratio < .1 & drop_soy==0 & pl_yrs_soy>5
		qui gen ps = iv_soy_recprice_state_real - ivps_median0	
		qui gen precXps = prec*ps
		qui gen prec2Xps = prec2*ps
		qui gen gdd8_26Xps = gdd8_26*ps
		qui gen gdd26_35Xps = gdd26_35*ps
		qui gen dday35CXps = dday35C*ps
		
		qui reghdfe acres_soy_ratio_harvest prec precXps prec2 prec2Xps gdd8_26 gdd8_26Xps gdd26_35 gdd26_35Xps dday35C dday35CXps ///
			i.stateansi#c.t i.stateansi#c.t2 yeart* if irr_ratio < .1 & drop_soy==0 & pl_yrs_soy>5, ///
			absorb(fips) 
		
		drop ps	ivps_median0 *Xps iv_soy_recprice_state_real
	
	end
	
	xtset, clear
	bootstrap,  cluster(stateyear) reps(1000): iv_price_soy		
	estimates store iv_price_soy

		* obtain effects evaluated at knots for plotting splines (Appendix Figure A11)
		lincom (gdd8_26 + 1.819*gdd8_26Xps)/100*(26-8)
		lincom (gdd8_26 + 1.819*gdd8_26Xps)/100*(26-8) + (gdd26_35 + 1.819*gdd26_35Xps)*(35-26)
		lincom (gdd8_26 + 1.819*gdd8_26Xps)/100*(26-8) + (gdd26_35 + 1.819*gdd26_35Xps)*(35-26) + (dday35C + 1.819*dday35CXps)*(41-35)

		lincom (gdd8_26 - 1.819*gdd8_26Xps)/100*(26-8)
		lincom (gdd8_26 - 1.819*gdd8_26Xps)/100*(26-8) + (gdd26_35 - 1.819*gdd26_35Xps)*(35-26)
		lincom (gdd8_26 - 1.819*gdd8_26Xps)/100*(26-8) + (gdd26_35 - 1.819*gdd26_35Xps)*(35-26) + (dday35C - 1.819*dday35CXps)*(41-35)

	* present 2nd-stage estimates	
	esttab iv_price_corn, b(4) se(5) keep(*dd* *prec*) order(gdd8* gdd26* dday* prec precX* prec2*)	
	esttab iv_price_soy, b(4) se(5) keep(*dd* *prec*) order(gdd8* gdd26* dday* prec precX* prec2*)	
	
* retrieve 1st-stage estimates

	qui reghdfe	corn_recprice_state_real L_corn_acres_state L_corn_stockvar c.year##c.year ///
		if acres_grain_ratio_harvest!=. & irr_ratio < .1 & drop_corn==0 & pl_yrs_corn>5, ///
		absorb(fips) vce(cluster stateansi)
	estimates store corn_fs	
				
	qui 
	reghdfe	soy_recprice_state_real L_soy_acres_state L_soy_stockvar c.year##c.year ///
		if acres_soy_ratio_harvest!=. & irr_ratio < .1 & drop_soy==0 & pl_yrs_soy>5, ///
		absorb(fips) vce(cluster stateansi) 	
	estimates store soy_fs
	
	esttab corn_fs soy_fs, se
